package com.ymatou.apigateway.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.SqProductDescUpdateBean;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.SqProductDescUpdateCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*更新商品图文详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SqProductDescUpdate{
	private static OpenapiReq openapiReq;
	private static SqProductDescUpdateBean sqproductdescupdateBean;
	private static SqProductDescUpdateCall sqproductdescupdateCall;

	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	private static IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
	

	// vip渠道
	private static int RootUserId = Integer.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品图文详情");
	}

	@Before
	public void caseUp() {
		sqproductdescupdateBean=new SqProductDescUpdateBean();
		sqproductdescupdateCall=new SqProductDescUpdateCall();
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("sq.product.desc.update");
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sqproductdescupdateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SqProductDescUpdate_001() {
		Logger.start(true,"更新商品图文详情");
		try {
			//前置数据
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            sqproductdescupdateBean.setDesc_pic_list(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
            sqproductdescupdateBean.setProduct_id(productId);
            sqproductdescupdateBean.setDescription("desc");
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) RootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqproductdescupdateBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.product.desc.update",
							app_id);
			sqproductdescupdateCall.setUrl(url);
			//openapi设置完成
			
			sqproductdescupdateCall.setData(openapiReq);
			sqproductdescupdateCall.callService();			
			Logger.verifyEquals("0000", sqproductdescupdateCall.getString("code"), "验证返回");

			List<Map<String, Object>> maps = ipm.selectDescConfigPictureByProdIdAndSellerId(productId, RootUserId);

			List<Map<String, Object>> pictures = maps.stream().filter(y->Integer.parseInt(y.get("iAction").toString())>0).collect(Collectors.toList());
			Logger.debug("pictures:"+pictures);
			for (int i = 0; i < sqproductdescupdateBean.getDesc_pic_list().size()-1; i++) {
				Logger.comment("验证pictures");
				Logger.verifyEquals(sqproductdescupdateBean.getDesc_pic_list().get(i+1), pictures.get(i).get("sOriUrl").toString(),
						"验证商品图片地址");
				Logger.verifyEquals(true, Integer.parseInt(pictures.get(i).get("iAction").toString()) > -1, "验证商品图片有效");
			}
			
			String maps1 = ipm.selectDescConfigWordByProdIdAndSellerId(productId, RootUserId).stream()
					.map(x -> x.get("WordContent").toString()).collect(Collectors.toList()).toString();
			Logger.debug(maps1);
			Logger.verifyEquals(true, maps1.contains(sqproductdescupdateBean.getDescription()), "验证Description");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}